
var settings = settings ||{};
var pageForm = new PageForm(settings||{});
function validate_login_form(thisform) {
	var username = $("#normalLoginForm input[name='username']").val();
	if (username == "") {
		alert("用户名不能为空！");
		return false;
	}
	var password = $("#normalLoginForm input[name='password']").val();
	if (password == "") {
		alert("密码不能为空！");
		return false;
	}
	var captcha = $("#normalLoginForm input[name='captcha']").val();
	if (captcha == "") {
		alert("验证码不能为空！");
		return false;
	}
	return true;
}
$script(window.global.plugin+'/jsrsasign/jsrsasign-all-min.js', 'keyutil',function(){

	var rsa=new RSAKey();
	var keyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpgdhUVnYUIaP1nigFVSAHPWGQZh1DNg0AX3Wy3xvcQ/+GumIAH5jKd+jo/8/+Ez4DSozETw1PhcBC42wezdCMggRxjBdPbqx2hwl5w79kuGKjxlV2ncaXXzn8gQwli+5a4oZvSx1n9Xq679CRrcSVk+MKvd2pqU6tChQ9FENIFQIDAQAB";
	keyStr = "30819f300d06092a864886f70d010101050003818d0030818902818100a981d85456761421a3f59e28055520073d6190661d43360d005f75b2df1bdc43ff86ba62001f98ca77e8e8ffcffe133e034a8cc44f0d4f85c042e36c1ecdd08c8204718c174f6eac76870979c3bf64b862a3c655769dc6975f39fc810c258bee5ae2866f4b1d67f57abaefd091adc49593e30abddda9a94ead0a143d144348150203010001";
	key = KEYUTIL.getKeyFromPublicPKCS8Hex(keyStr);
	console.log(key)
	var key = new RSAKey();
	  key.readPKCS8PubKeyHex(keyStr);
	console.log(key.encrypt("123456"))
});
function render(){
	var oper = $("#showErrMsg").attr("oper");
	if(oper=="0"){
		$("#showErrMsg").addClass("hide");
	}else{
		var val = $("#showErrMsg").attr("val");
		if(val=="200"){
			$("#showErrMsg").removeClass("hide");
			$("#success").show().html($("#showErrMsg").find("div").html()).prev(".widget-body").hide();
		}else if(val==""){
			
		}else{
			$("#showErrMsg").removeClass("hide");
		}
	}
}
pageForm.formCheck = function(form,e){//
	if(!form.validationEngine("validate")){
		  return false;
	}if(!validate_login_form(form)){
		  return false;
	}
	return true;
};
pageForm.formBeforeSubmit = function(form,e){//
   var $target = form.find("button[type='submit']");
   console.log($target)
   $target.attr("disabled",true);
   $target.attr("normal-title",$target.html());
   var t = $target.attr("dealing-title");
   if(t){
	   $target.text(t+"...");
   }else{
	   $target.text($target.html()+"中...");
   }
};
pageForm.formSuccess = function(form,data){//
	$("#showErrMsg").attr("oper",data.oper);
	if(data.status!=200){
		 $("#showErrMsg").attr("oper","");
	     $("#showErrMsg").attr("val",data.status);
	     $("#showErrMsg").find("div").html(data.desc);
	     render();
	     $("#randCodeImage").click(); 
	}else{
		var action = window.global.getFinalUrl(window.global.ctx+"/index");
		window.location.href=action;
	}
};
pageForm.formComplete = function(form,XMLHttpRequest, textStatus){//
   var $target = form.find("button[type='submit']");
   $target.html($target.attr("normal-title"));
   $target.attr("disabled",false);
};
pageForm.formError = function(form,xhr, status, error){//
	$("#showErrMsg").attr("oper",data.oper);
	if(data.status!=200){
		$("#showErrMsg").attr("oper","");
	}
     $("#showErrMsg").attr("val",data.status);
     $("#showErrMsg").find("div").html(data.desc);
     render();
     $("#randCodeImage").click();  
}; 
pageForm.formDataInit = function(form,e){//
	var password = $("#pw").val();
	if (password == "") {
		alert("密码不能为空！");
		return false;
	}
	var message = password;
	var ciphertext = encryptByAes(message, aesKey);
	$("#normalLoginForm input[name='password']").val(ciphertext)
	return true;
};
$(function() {
	render();
    $('input[type=checkbox]').iCheck({
	    checkboxClass: 'icheckbox_square-blue',
	    radioClass: 'iradio_square-blue',
	    increaseArea: '20%'
   }).on('ifChecked', function(e){
		 $(this).prop("checked", true)
	 }).on('ifUnchecked', function(e){
		 $(this).prop("checked", false)
	 });
    $("#pw").val($("#normalLoginForm input[name='password']").val())
    $("#normalLoginForm input[name='password']").on("keyup",function(e){
    	$("#pw").val($(this).val())
    })
	pageForm.init({formId:"normalLoginForm"})
});
/**
 * 加密数据
 * @param {type} message 待加密的字符串
 * @param {type} key 秘钥
 * @returns {unresolved} 加密后的数据
 */
function encryptByAes(message, key) {
	var sendData = CryptoJS.enc.Utf8.parse(message);
    var key = CryptoJS.MD5(key);
    var encrypted = CryptoJS.AES.encrypt(sendData, key,{iv:key,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Iso10126});
    return encrypted.toString();
} 